<?php
namespace app\controllers;
use app\models\Class2;
use app\models\DataRelationship;
use app\models\Notebook;
use app\models\ReadContent;
use app\models\ReadOption;
use app\models\ReadQuestion;
use \yii\web\Controller;

class ReadingController extends Controller
{
    public $layout='reading';
    public function beforeAction($action)
    {
        $session = \Yii::$app->session;
        $session->open();
        if (!isset($session['user_id'])) {
            if(isset($_COOKIE['dianke_user_id']))
                $this->redirect('?r=site/dologin&islogin='.$_COOKIE['dianke_user_id']);
            else
                $this->redirect('?r=site/login');
        }
        return parent::beforeAction($action);
    }



    public function actionIndex()
    {
        $session = \Yii::$app->session;
        $session->open();
        $request = \Yii::$app->request;
        if($request->isAjax)
        {
            $note=$request->post('saved-note');
            $my_note=$session['read_note'];
            $my_note->note=$note;
            $my_note->save();
            $session['read_note']=$my_note;
        }
        elseif($request->isPost)
        {
            $post_type=$request->post('post_type');
            $que = $request->post('que');//请求下一题的题号
            $time_interval=$request->post('time');
            if($time_interval=="")
            {
                $time_interval=1;
            }
            $que_option=$session['que_option'];
            $user_key=$session['user_key'];
            $t_time=$session['t_time'];
            $my_note=$session['read_note'];
            if(isset($t_time[$que-1]))
            {
                $t_time[$que-1]+=$time_interval;
            }else
            {
                $t_time[$que-1]=$time_interval;
            }
            $session['t_time']=$t_time;
            $que_option[$que-1]=$request->post('que_option');//用户自选的题型
            $uk=json_decode($request->post('user_key'));
            $user_key[$que-1]=$uk;//用户选择的关键句id
            $session['que_option']=$que_option;
            $session['user_key']=$user_key;
            $answer=$session['read_user_answer'];
            //存下用户的答案
            if($que>1){
                $answer[$que-1]=json_decode($request->post('usercheck'));
                $session['read_user_answer']=$answer;
                $state="next";//时间状态
            }else {
                if(!isset($session['read_user_answer']))
                    $state="begin";
                else
                    $state="next";
            }
            if($post_type=='goon')
            {
                //方法练习选项
                if (!isset($session['read_practice'])) {
                    $checked = $request->post('waytopra');
                    if (isset($checked))
                        $session['read_practice'] = true;
                    else
                        $session['read_practice'] = false;
                }
                $read_content = $session['read_content'];
                $read_question =
                    ReadQuestion::find()
                        ->where(['tnum' => $que, 'read_content_id' => $read_content['id']])
                        ->one();
                $read_question_num = $session['read_question_num'];
                $read_option = ReadOption::find()->where(['read_question_id' => $read_question['id']])->all();
                if (isset($read_question))
                {
                    $result=['read_content' => $read_content,
                        'read_question' => $read_question,
                        'read_option' => $read_option,
                        'que' => $que + 1,
                        'read_question_num' => $read_question_num,
                        'class2_name' => $session['class2_name'],
                        'answer'=>$answer,
                        'que_option'=>$que_option,
                        'user_key'=>$user_key,
                        'state'=>$state,
                        'token'=>$session['time_token'],
                        'my_note'=>$my_note
                    ];
                    switch ($read_question['type'])
                    {
                        case 1:
                            if(!$session['read_practice']||$read_question['t_type']==5||$read_question['t_type']==7)
                                return $this->render('1', $result);
                            else
                                return $this->render('2', $result);
                            break;
                        case 2:
                            return $this->render('2', $result);
                            break;
                        case 3:
                            return $this->render('3', $result);
                            break;
                        case 4:
                            return $this->render('4', $result);
                            break;
                    }
                }
                else
                {
                    //展示答案
                    $session['reading_complete']=true;
                    //存入关系表
                    $this->saveState();
                    return $this->redirect('?r=community/reading');
                }
            }
            elseif($post_type=='goback')
            {
                $que-=2;//回退题号
                if($que>0)
                {
                    $read_content = $session['read_content'];
                    $read_question = ReadQuestion::find()->where(['tnum' => $que, 'read_content_id' => $read_content['id']])->one();
                    $read_question_num = $session['read_question_num'];
                    $read_option = ReadOption::find()->where(['read_question_id' => $read_question['id']])->all();
                    if (isset($read_question)) {
                        $result=['read_content' => $read_content,
                            'read_question' => $read_question,
                            'read_option' => $read_option,
                            'que' => $que + 1,
                            'read_question_num' => $read_question_num,
                            'class2_name' => $session['class2_name'],
                            'answer'=>$answer,
                            'que_option'=>$que_option,
                            'user_key'=>$user_key,
                            'state'=>$state,
                            'token'=>$session['time_token'],
                            'my_note'=>$my_note
                        ];
                        switch ($read_question['type']) {
                            case 1:
                                if(!$session['read_practice'])
                                    return $this->render('1', $result);
                                else
                                    return $this->render('2', $result);
                                break;
                            case 2:
                                return $this->render('2', $result);
                                break;
                            case 3:
                                return $this->render('3', $result);

                                break;
                            case 4:
                                return $this->render('4', $result);
                                break;
                        }
                    }
                }else
                {
                    //回退到driction
                    $this->layout='direction';
                    return $this->render('direction',['class2_name'=>$session['class2_name'],'class_num'=>$session['read_content']['class_num']]);
                }
            }
        }else
        {
            $this->layout='direction';
            $session = \Yii::$app->session;
            $session->open();
            unset($session['read_practice']);
            unset($session['read_user_answer']);
            unset($session['que_option']);
            unset($session['user_key']);
            unset($session['t_time']);
            unset($session['read_note']);
            $session['reading_complete']=false;
            $request = \Yii::$app->request;
            $id=$request->get('id');
            //todo
            $read_content=ReadContent::findOne($id);
            $read_question=ReadQuestion::find()->where(['read_content_id'=>$read_content['id']])->
            orderBy('tnum')->all();
            $class2=Class2::findOne($read_content['class2_id']);
            $where=[
                'class1_id'=>$read_content['class1_id'],
                'class2_id'=>$read_content['class2_id'],
                'question_id'=>$read_content['id'],
                'user_id'=>$session['user_id']
            ];
            $my_note=Notebook::find()->where($where)->one();
            if(!isset($my_note))
            {
                $my_note=new Notebook();
                $my_note->class1_id=$read_content['class1_id'];
                $my_note->class2_id=$read_content['class2_id'];
                $my_note->question_id=$read_content['id'];
                $my_note->user_id=$session['user_id'];
                $my_note->save();
            }
            $session['read_note']=$my_note;
            $session['read_content']=$read_content;
            $session['read_question_num']=count($read_question);
            $session['class2_name']=$class2['name'];
            $session['read_question']=$read_question;
            $session['time_token']=md5("begin~".time());//开始计时token
            return $this->render('direction',['class2_name'=>$class2['name'],'class_num'=>$read_content['class_num']]);
        }
    }
    public function actionReview(){
        $this->layout='review';
        return $this->render('review');
    }

    /**
     * 将做题完成状态存入关系表
     */
    private function saveState()
    {
        $session=\Yii::$app->session;
        $session->open();
        $where=[
            'user_id'=>$session['user_id'],
            'class1_id'=>$session['read_content']['class1_id'],
            'class2_id'=>$session['read_content']['class2_id'],
            'content_id'=>$session['read_content']['id']
        ];
        $data_relation=DataRelationship::find()->where($where)->one();
        if(!isset($data_relation))
        {
            //新建一条数据
            $data_relation=new DataRelationship();
            $data_relation->user_id=$session['user_id'];
            $data_relation->class1_id=$session['read_content']['class1_id'];
            $data_relation->class2_id=$session['read_content']['class2_id'];
            $data_relation->content_id=$session['read_content']['id'];
        }
        $data_relation->answer=serialize($session['read_user_answer']);
        $data_relation->user_key=serialize($session['user_key']);
        $data_relation->t_time=serialize($session['t_time']);
        $data_relation->last_time=date('Y-m-d H:i:s');
        $data_relation->reading_practice=serialize($session['read_practice']);
        $data_relation->que_option=serialize($session['que_option']);
        $data_relation->save();
        $read_content=$session['read_content'];
        $where=[
        'class1_id'=>$read_content['class1_id'],
        'class2_id'=>$read_content['class2_id'],
        'content_id'=>$read_content['id']
        ];
        //更新做题人数
        $rows=DataRelationship::find()->select(['id'])->where($where)->asArray()->all();
        $read_content->num=count($rows);
        $read_content->save();
    }
}